import util from "../../utils/util.js";
var app = getApp();
const innerAudioContext = wx.getBackgroundAudioManager();
Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        //轮播图属性设置
        indicatorDots: true,
        autoplay: false,
        interval: 3000,
        duration: 500,
		detail:{},
		region:["广东省","广州市","荔湾区"],
        pageNumber: 1,
        readMoreFlag: false,
        imgHeight:0,
        audioData: {
            bookid: "",
            name: "",
            img: "",
            index: 0
        },
        isSign:"1"
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var userInfo = wx.getStorageSync("userInfo");
        var that = this;
        if(!userInfo || userInfo == ""){
            wx.switchTab({
                url: '../mine/mine',
            })
            app.globalData.isShouQuan = false;
        }
        var system = wx.getSystemInfoSync();
        this.setData({
            imgHeight:300*system.screenWidth/750,
            left:system.windowWidth-60,
            top:system.windowHeight-60
        });
        this.getDetail();
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.checkPlayAudio();
    },
	//获取列表信息
	getDetail:function(){
		var that = this;
		util.wxAjax("glBook/list","post",{},function(res){
			var data = res.data;
			if(data.result == 200 || data.result == "200"){
                var jsonData = data.data;
                for(var i=0;i<jsonData.books.length;i++){
                    if(jsonData.books[i].hits > 999){
                        jsonData.books[i].hits = 999;
                    }
                }
                if (jsonData.books.length > 0){
                    var temp = jsonData.books[0];
                    var audioData = {
                        bookid: temp.id,
                        name: temp.bookName,
                        img: temp.img,
                        index: 0
                    }
                    that.setData({
                        audioData: audioData
                    });
                    wx.setStorageSync("audioData", JSON.stringify(audioData));
                }
                that.setData({
                    detail: jsonData,
                    isSign: data.data.isSign,
                });
            }
		});
	},
    //上拉刷新
    onPullDownRefresh: function () {
        var system = wx.getSystemInfoSync();
        // 显示顶部刷新图标  
        wx.showNavigationBarLoading();
        this.setData({
            pageNumber: 1,
            detail: {},
            left: system.windowWidth - 60,
            top: system.windowHeight - 60
        });
        this.getDetail();
    },
    //判断是否有音乐在播放
    checkPlayAudio:function(){
        var time = innerAudioContext.currentTime;
        var bookid = wx.getStorageSync("listenBookId");
        var name = wx.getStorageSync("listenBookName");
        var img = wx.getStorageSync("listenBookImg");
        var index = wx.getStorageSync("listenBookIndex");
        var audioData = {};
        var that = this;
        setTimeout(function () {
            var newTime = innerAudioContext.currentTime;
            try {
                if (newTime >= time) {
                    audioData = {
                        bookid: bookid,
                        name: name,
                        img: img,
                        index: index
                    };
                }else {
                    var temp = wx.getStorageSync("audioData");
                    if(!!temp && temp!=""){
                        temp = JSON.parse(temp);
                    }
                    audioData = {
                        bookid: temp.bookid,
                        name: temp.name,
                        img: temp.img,
                        index: 0
                    };
                }
                that.setData({
                    audioData: audioData
                });
            } catch (e) {
                var temp = wx.getStorageSync("audioData");
                if (!!temp && temp != "") {
                    temp = JSON.parse(temp);
                    that.setData({
                        audioData: temp
                    });
                }else{
                    that.setData({
                        audioData: {
                            bookid: "",
                            name: "",
                            img: "",
                            index: 0
                        }
                    });
                }
            }
        }, 400);
    },
    viewTouchMove:function(e){
        var system = wx.getSystemInfoSync();
        var left = e.touches[0].clientX;
        var top = e.touches[0].clientY;
        var width = system.windowWidth - 20;
        var height = system.windowHeight - 20;
        if (left < 0) {
            left = 0;
        }
        if (top < 0) {
            top = 0
        }
        if (width < left) {
            left = width;
        }
        if (height < top) {
            top = height;
        }
        this.setData({
            left: left - 25,
            top: top - 25
        })
    },
})
